package cc.momas.momall.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jms.Destination;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cc.momas.common.utils.IDUtils;
import cc.momas.common.vo.EasyUIDataGridResult;
import cc.momas.momall.mapper.TbItemDescMapper;
import cc.momas.momall.mapper.TbItemMapper;
import cc.momas.momall.po.TbItem;
import cc.momas.momall.po.TbItemDesc;
import cc.momas.momall.po.TbItemExample;
import cc.momas.momall.service.IItemService;

@Service("itemService")
public class ItemServiceImpl implements IItemService{

	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper itemDescMapper;
	@Autowired
	private JmsTemplate jmsTemplate;
	@Resource
	private Destination topicDestination;
	
	
	
	/**
	 * 测试用,不要使用这个
	 */
	@Deprecated
	@Override
	public List<TbItem> queryAll() {
		return itemMapper.selectByExample(null);
	}

	/**
	 * 分页查询,page表示当前第page页,rows表示一页包含rows行数据
	 */
	@Override
	public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
		//设置分页信息
		PageHelper.startPage(page, rows);
		//设置查询条件
		TbItemExample example = new TbItemExample();
		// ---
		// 这里应该设置查询条件,但这里并没有需求,故为空
		// ---
		//执行查询
		List<TbItem> list = itemMapper.selectByExample(example);
		//获取分页结果
		PageInfo<TbItem> info = new PageInfo<>(list);
		//把查询到的结果进行封装,以供返回
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(list);
		result.setTotal(info.getTotal());
		
		return result;
	}

	/**
	 * 根据主键查询商品
	 */
	@Override
	public TbItem getItemById(Long itemId) {
		TbItem item = itemMapper.selectByPrimaryKey(itemId);
		return item;
	}

	/**
	 * 添加商品
	 */
	@Override
	public int save(TbItem item, String desc, String itemParams) {
		int count = 0;
		
		long itemId = IDUtils.genItemId();
		
		item.setId(itemId);
		item.setStatus(TbItem.INSTOCK);
		item.setCreated(new Date());
		item.setUpdated(new Date());
		count += itemMapper.insert(item);
		
		TbItemDesc itemDesc = new TbItemDesc();
		itemDesc.setItemId(itemId);
		itemDesc.setCreated(new Date());
		itemDesc.setUpdated(new Date());
		itemDesc.setItemDesc(desc);
		count += itemDescMapper.insert(itemDesc);
		//itemParams 商品规格,未使用
		
		//发送商品添加消息
		jmsTemplate.send(topicDestination,(session) -> session.createTextMessage(itemId + ""));
		
		System.out.println("已成功添加商品并发送消息,影响" + count + "行数据");
		
		return count;
	}

	@Override
	public int delete(String ids) {
		String[] id = ids.split(",");
		int count = 0;
		TbItem item = null;
		for (int i = 0; i < id.length; i++) {
			item = new TbItem();
			item.setId(Long.valueOf(id[i]));
			item.setStatus(TbItem.DELETED);
			count += itemMapper.updateByPrimaryKeySelective(item);
		}
		return count;
	}

	/**
	 * 下架商品
	 */
	@Override
	public int updateInStock(String ids) {
		String[] id = ids.split(",");
		int count = 0;
		TbItem item = null;
		for (int i = 0; i < id.length; i++) {
			item = new TbItem();
			item.setId(Long.valueOf(id[i]));
			item.setStatus(TbItem.OUTSTOCK);
			count += itemMapper.updateByPrimaryKeySelective(item);
		}
		return count;
	}

	/**
	 * 上架商品
	 */
	@Override
	public int updateReshelf(String ids) {
		String[] id = ids.split(",");
		int count = 0;
		TbItem item = null;
		for (int i = 0; i < id.length; i++) {
			item = new TbItem();
			item.setId(Long.valueOf(id[i]));
			item.setStatus(TbItem.INSTOCK);
			count += itemMapper.updateByPrimaryKeySelective(item);
		}
		return count;
	}

	/**
	 * 根据id修改item的相关属性和对象
	 */
	@Override
	public int updateItemByPrimaryKeySelective(TbItem item, String desc, String itemParams, String itemParamId) {
		int count = 0;
		item.setUpdated(new Date());
		count += itemMapper.updateByPrimaryKeySelective(item);
		
		TbItemDesc itemdesc = new TbItemDesc();
		itemdesc.setItemId(item.getId());
		itemdesc.setItemDesc(desc);
		itemdesc.setUpdated(new Date());
		count += itemDescMapper.updateByPrimaryKeySelective(itemdesc);
		
//		TbItemParam param = new TbItemParam();
//		param.setId(item.getId());
//		param.setParamData(desc);
		
		return count;
	}



	
}
